<?php

namespace backend\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\models\Factory;
use backend\models\AdminInfo;
use yii\data\Sort;

/**
 * SysItemSearch represents the model behind the search form about `backend\models\SysItem`.
 */
class FactorySearch extends Factory {
/**
     * @inheritdoc
     */
		 public function rules()
		    {    	
		       return [
		            [['id' ], 'integer'],
		            [['name'], 'string', 'max' => 24],
		        	[['short_code','bank_account'], 'string', 'max' => 50],
		            [['addr','delivery_address'], 'string', 'max' => 128],
		        	[['email','phone','bank_name','delivery_tel','Responsible_tel'], 'string', 'max' => 64],
		        	[['contacts','Responsible_person',], 'string', 'max' => 20],
		        		
		        ];
		    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
    	
    	$query = Factory::find();
        $dataProvider = new ActiveDataProvider([
            'query' => $query,
	        'pagination'=>['pageSize' => '10'],
        ]);
      /*   $dataProvider->Sort->attributes['id'] = [
                    'asc' => ['name' => SORT_ASC],
                    'desc' => ['name' => SORT_DESC],
                ]; */
        $this->load($params);

        if (!$this->validate()) {
            return $dataProvider;
        }

        $query->andFilterWhere([
            'id' => $this->id
        ]);
        $query->andFilterWhere(['like', 'name', $this->name]) 
        		->andFilterWhere(['like', 'bank_account', $this->bank_account])
        		->andFilterWhere(['like', 'email', $this->email])
        		->andFilterWhere(['like', 'phone', $this->phone])
        		->andFilterWhere(['like', 'bank_name', $this->bank_name])
        		->andFilterWhere(['like', 'contacts', $this->contacts])
            ->andFilterWhere(['like', 'short_code', $this->short_code])
            ->andFilterWhere(['like', 'addr', $this->addr])
           ;
        return $dataProvider;
    }
    
}
